/*
 * @Author:Iyoole
 * @Time  :2017-11-29
 * @Description:路由
 * @git:
 * */
;!function(e){
    "use strict";
    var i,c={
        gu:function(){
            var h = location.hash.split("?"),
                n = h[0].split("#")[1],//路由地址
                p = h[1] ? h[1].split("&") : [],//参数内容
                q = {};
            for(var x = 0;x<p.length ; x++){
                var m = p[x].split("=");
                q[m[0]] = m[1]
            }
            return  {
                path:n,
                query:q
            }
        },
        isFun:function(cb){
            return Object.prototype.toString.call(cb)==='[object Function]'
        }

    };
    var router=function(){
        this.r={};
        this.bf=null;
        this.af=null;
    };
    router.pt=router.prototype;
    router.pt.render=function(){
        var t=this;
        window.addEventListener('load',function(){
            t.uc()
        });
        window.addEventListener('hashchange',function(){
            t.uc()
        });
        window.SPA_RESOLVE_INIT=null;
    };
    router.pt.uc=function(){
        var nh=c.gu();
        if(this.r[nh.path]){
            this.refresh(nh)
        }else{
            location.hash='/index'
        }
    };
    router.pt.refresh=function(nh){
        var t=this;
        if(t.bf){
            t.bf({
                to:{
                    path:nh.path,
                    query:nh.query
                },
                next:function(){
                    t.r[nh.path].callback.call(t,nh)
                }
            })
        }else{
            t.r[nh.path].callback.call(t,nh)
        }
    };
    router.pt.beforeEach=function(callback){
        if(c.isFun(callback)){
            this.bf=callback
        }else{
            console.trace('路由切换前勾子函数不正确');
        }
    };
    router.pt.afterEach=function(callback){
        if(c.isFun(callback)){
            this.af=callback;
        }else{
            console.trace('路由切换后回调函数不正确')
        }
    };
    router.pt.map=function(path,callback){
        path=path.replace(/\s*/g,'');
        if(callback&&c.isFun(callback)){
            this.r[path]={
                callback:callback,
                fn:null
            }
        }else{
            console.trace('注册'+path+'地址需要提供正确的注册回调');
        }
    };
    router.pt.asyncFun=function(file,transition){
        var t=this;
        if(t.r[transition.path].fn){
            t.af&&t.af(transition);
            t.r[transition.path].fn(transition)
        }else{
            console.log('开始异步下载js'+file);
            var _b=document.getElementsByTagName('body')[0];
            var _s=document.createElement('script');
            _s.type='text/javascript';
            _s.src=file;
            _s.async=true;
           // SPA_RESOLVE_INIT=null;
            _s.onload=function(){
                console.log('下载'+file+'完成');
                t.af&&t.af(transition);
                t.r[transition.path].fn=SPA_RESOLVE_INIT;
                t.r[transition.path].fn(transition);
            };
            _b.appendChild(_s)
        }
    };
    router.pt.syncFun=function(callback,transition){
        this.af&&this.af(transition);
        callback&&callback(transition)
    };

    var JsRouter=new router();

    JsRouter.v='1.0.1';
    if(window.fui && fui.define ){
        fui.define('jquery', function(exports){
            e.JsRouter=JsRouter;
            i=fui.$;
            exports('JsRouter', JsRouter);
        });
    }else{//如果直接引用的话
        e.JsRouter=JsRouter;
        i=e.jQuery;
    }
}(window);








